package Test;

import java.io.*;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Random;


class Student{
    public String name;
    public Score scores;

    //Student构造函数
    public Student(String name , Score score){
        this.name = name;
        this.scores = score;

    }


    public int compareTo(Student stu_1) {
        if(this == stu_1){
            return 0;
        }
        else if(stu_1 != null && stu_1 instanceof Student)
        {
            Student stu_2 = (Student) stu_1;
            if(scores.sum <=stu_2.scores.sum){
                return -1;
            }else {
                return -1;
            }
        }
        else return -1;
    }


}

class Score {
    public int[] score;
    public int sum;

    //Score构造函数,如果成绩过多可用list存取
    public Score(int[] grade, int sum) {

        this.score = grade;
        this.sum = sum;
    }
}


    public class Test {
        private ArrayList<String> list = new ArrayList<>();

        //读取txt文件内容
        public String readFile(File file) {
            StringBuilder result = new StringBuilder();
            try {
                //构造一个BufferedReader类来读取文件,并且将java工程中的文本编码设置成gbk
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
                String s = null;
                //使用readLine方法，一次读一行
                while ((s = br.readLine()) != null) {
                    result.append(System.lineSeparator() + s);
                    list.add(s);


                }
                System.out.println(result);
                br.close();

            } catch (Exception e) {
                e.printStackTrace();
            }
            return result.toString();
        }

        //向txt文件写入内容
        public void printFile(String fileName) throws Exception {

            try {
                FileWriter fileWriter = new FileWriter(new File(fileName), true);
                BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName, true), "gbk"));
                for (int i = 0; i < list.size(); i++) {
                    bufWriter.write(String.valueOf(list));
                    bufWriter.newLine();
                }
                bufWriter.flush();
                fileWriter.flush();
                bufWriter.close();
                fileWriter.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}


        //产生一个新的成绩册
        public String inputScore(File file) {
            ArrayList<Student> students = new ArrayList();
            int size = students.size();
            StringBuilder result = new StringBuilder();
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
                // Stream(file),"UTF-8"));
                String s = null;
                while ((s = br.readLine()) != null) {
                    String name = null;
                    String[] ss = s.split(" \\s ");
                    result.append(System.lineSeparator() + s);

                    //添加成绩
                    int[] grade1 = {0, 0, 0, 0, 0};
                    int sum = 0;
                    int []aver = {0, 0, 0, 0, 0};

                    for (int i = 0; i < 5; i++) {
                        Random rod = new Random();
                        grade1[i] = (int) (75 + 5 * rod.nextGaussian());
                        sum += grade1[i];
                        aver[i]+= grade1[i];
                    }
                    Score score_1 = new Score(grade1, sum);
                    //读取到students里
                    Student stu = new Student(name, score_1);
                    stu.name = ss[0];
                    stu.scores = score_1;
                    students.add(stu);
                    System.out.println(stu.name);

                    //排序
                    if(students.size() > 1){
                        for(int k = 0; k<students.size(); k++) {
                            for (int j = 0; j < k; j++) {
                                if (students.get(k).compareTo(students.get(j)) < 0) {
                                    Student temp = students.get(k);
                                    students.set(k, students.get(j));
                                    students.set(j, temp);
                                }
                            }
                        }
                    }
                    //写入数据
                    try {

                        BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
                        bufWriter.write(" 姓名\t学号\t语文\t数学\t外语\t物理\t化学\t总成绩 ");
                        bufWriter.newLine();
                        for (int j = 0; j < 35; j++) {
                            bufWriter.write(students.get(j).name);
                            bufWriter.write("\t");

                            for (int m = 0; m < 5; m++) {
                                bufWriter.write(String.valueOf(students.get(j).scores.score[m]));
                                bufWriter.write("  ");
                            }
                            bufWriter.write(String.valueOf(students.get(j).scores.sum));
                            bufWriter.write("  ");

                            bufWriter.newLine();

                        }
                        for(int n = 0; n<5; n++){
                            bufWriter.write(aver[n]);
;                        }


                        bufWriter.flush();
                        bufWriter.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                br.close();


            } catch (Exception e) {
                e.printStackTrace();
            }
            return students.toString();


        }


        public static void main(String[] args) throws Exception {
            Test fileTest = new Test();
            File file_1 = new File("2015-class6花名册.txt");
            fileTest.readFile(file_1);
            fileTest.printFile("score_1.txt");
            fileTest.inputScore(file_1);
            //fileTest.arr();


        }


}


/*

 注释：
                     int num = 0;
                     /* while (num <35){
                          bufWriter.write(students.get(num).name);
                          for(int i = 0 ; i< 5;i++){
                              bufWriter.write(String.valueOf(students.get(num).scores.get(i)));
                          }
                  }*/
                  //写入成绩
                /*  bufWriter.write(ss[0]);
                  //格式化
                  if (ss[0].length() == 2) {
                      bufWriter.write("   ");
                  } else if (ss[0].length() == 3) {
                      bufWriter.write("  ");
                  }
                  //bufWriter.write(ss[1]);
                  for(int i = 0; i<5;i++)
                  bufWriter.write(stu.scores.get(i).score);*/

//正态分布函数
   /* public  double normalRandom(double a, double b) {
        double temp = 12;
        double x = 0 ;
        for (int i = 0; i < 2; i++)
        {
            x = x + (Math.random());
            x = (x - temp / 2) / (Math.sqrt(temp / 12));
            x = a + x * Math.sqrt(b);

        }
        return x;
    }*/

   //按空格划分读取范围
/* int idx;
                  idx = s.indexOf("\\s");
                  String s0 = s.substring(0 , idx);
                  String s1 = s.substring(idx + 1, s.length() - 1);
                  String[] ss = { s0, s1 };*/
//setScore方法
 /* public setScore(){
        double sum=0;
        for (int i=0;i<5;i++){
            Score s=new Score();

            Random rod = new Random();
            s.score = rod.nextGaussian();
            scores.add(s);
            sum+=s.score;

        }
        Score sum0=new Score();
        sum0.score=(sum);
        scores.add(sum0);*/

